function varargout = TLSwitchDlg(action,varargin)

% TL_SWITCH_DLG manages the user interface for TargetLink Switch block
%
% SYNTAX dlgInfo = TLSwitchDlg('GetDlgInfo');
%   gets dialog info of TargetLink Switch block
%
%   INPUT ARGUMENTS
%   -/-      
%
%   OUTPUT ARGUMENTS
%   dlgInfo     dialog information contains pageNames etc.
%
% SYNTAX [sheetDims, ctrlPos] = TLSwitchDlg('CtrlPositions', pageName, commonGeom, dlgInfo, sheetDims);
%   calculates the control positions of all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   commonGeom      common geometry data
%   dlgInfo         dialog information contains pageNames etc.
%   sheetDims       dimension of sheet
%
%   OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% SYNTAX dlgdata = TLSwitchDlg('Create', pageName, dlgfig, dlgdata);
%   creates all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX [dlgdata,bModified] = TLSwitchDlg('Manage', pageName, dlgfig, dlgdata, pageAction);
%   manages all actions of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageAction      modified control of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%   bModified       flag = 1, if dialog data are modified, =0 otherwise
%
% SYNTAX dlgdata = TLSwitchDlg('Update', dlgfig, dlgdata, pageNum);
%   updates all controls of dialog page: <pageNum>
%
%   INPUT ARGUMENTS
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageNum         number of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX TLSwitchDlg('Apply', dlgfig, dlgdata);
%   writes current blockdata to block and DD
%
%   INPUT ARGUMENTS
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
%   OUTPUT ARGUMENTS
%   - / -


% Author(s): D. Andoleit, T. Pietzsch
% $RCSfile: TLSwitchDlg.m $
% $ProjectName: e:/ARC/Components/HostSW/SimulinkBlocksets/TLBlocksets/CommonBlockset/CommonBlockset_SimBlocksUI/Sources/MSrc/project.pj $
% $Revision: 1.26 $
% $Date: 2011/07/21 15:36:03MESZ $
%
% Copyright ?2011 dSPACE GmbH. All rights reserved.


switch action
    case 'GetDlgInfo'
        hBlock = varargin{1};
        dlgInfo = tl_get_dlginfodefaults('tlsim');
        titleName = 'Switch';
        pathinfo = tl_pathinfo(hBlock);
        dlgInfo.title = sprintf('TargetLink %s: %s', titleName, pathinfo.shortDisplayName);
        dlgInfo.pageNames   = {
            'Output' ...
            'Threshold' ...
            'Logging & Autoscaling' ...
            'Documentation'};
        varargout{1} = dlgInfo;
        varargout{2} = get_tlcg_data(hBlock);

    case 'CtrlPositions'
        pageName     = varargin{1};
        commonGeom   = varargin{2};
        sheetDims    = varargin{4};
        switch pageName
            case 'Output'
                excludedCtrls = { };
                [sheetDims, ctrlPos] = tl_calc_ctrls_positions(commonGeom, excludedCtrls);

            case 'Threshold'
                [sheetDims,ctrlPos] = FcnCalcThresholdControlPositions(commonGeom);

            case 'Logging & Autoscaling'
                excludedCtrls = {};
                [sheetDims, ctrlPos] = ...
                    TLManageLoggingCtrls('calcCtrls', commonGeom, sheetDims, excludedCtrls);

            case 'Documentation'
                bTblBlock = 0;
                bCodeComment = 1;
                [sheetDims,ctrlPos] = ...
                    TLManageDocCtrls('ctrlpos',commonGeom,sheetDims,bCodeComment,bTblBlock);
        end
        varargout{1} = sheetDims;
        varargout{2} = ctrlPos;

    case 'Create',
        pageName = varargin{1};
        dlgfig   = varargin{2};
        dlgdata  = varargin{3};
        pageNum  = strmatch(pageName,dlgdata.pageNames,'exact');
        switch pageName
            case 'Output'
                % create all controls
                dlgdata = TLManageVariableCtrls('create',pageNum,dlgfig,dlgdata);
                dlgdata = TLManageScalingCtrls('create',pageNum, dlgfig,dlgdata);

            case 'Threshold'
                dlgdata = FcnCreateThresholdControls(pageNum,dlgfig,dlgdata);

            case 'Logging & Autoscaling'
                % set bCheckLogging, because block supports bus signals
                dlgdata.pageData{pageNum}.bCheckLogging = 1;
                dlgdata = TLManageLoggingCtrls( ...
                    'create', pageNum, dlgfig, dlgdata);

            case 'Documentation'
                dlgdata = TLManageDocCtrls( ...
                    'create', pageNum, dlgfig, dlgdata);
        end
        dlgdata.pageData{pageNum}.ctrlPos = [];
        varargout{1} = dlgdata;

    case 'Manage',
        pageName   = varargin{1};
        dlgfig     = varargin{2};
        dlgdata    = varargin{3};
        pageAction = varargin{4};
        pageNum    = strmatch(pageName, dlgdata.pageNames, 'exact');
        switch pageName
            case 'Output'
                [dlgdata, bModified1] = TLManageVariableCtrls( ...
                    'manage', pageNum, dlgfig, dlgdata, pageAction, 'output');
                [dlgdata, bModified2] = TLManageScalingCtrls( ...
                    'manage', pageNum, dlgfig, dlgdata, pageAction, 'output');
                bModified = bModified1 || bModified2;

            case 'Threshold'
                [dlgdata, bModified] = FcnManageThresholdControls( ...
                    pageNum, dlgfig, dlgdata, pageAction);

            case 'Logging & Autoscaling'
                [dlgdata, bModified] = ...
                    TLManageLoggingCtrls('manage', pageNum, dlgdata, pageAction, 'logdata');

            case 'Documentation'
                [dlgdata, bModified] = TLManageDocCtrls( ...
                    'callback', pageNum, dlgfig, dlgdata, pageAction);
        end
        varargout{1} = dlgdata;
        varargout{2} = bModified;

    case 'ManageSystemIcons',
        dlgfig     = varargin{1};
        dlgdata    = varargin{2};
        pageAction = varargin{3};
        [dlgdata, bModified] = FcnManageSystemIcons(dlgfig,dlgdata,pageAction);
        varargout{1} = dlgdata;
        varargout{2} = bModified;

    case 'Update'
        dlgfig    = varargin{1};
        dlgdata   = varargin{2};
        pageNum   = varargin{3};

        pageName  = dlgdata.pageNames{pageNum};

        switch pageName
            case 'Output'
                % update all controls
                dlgdata = TLManageVariableCtrls( ...
                    'update', pageNum, dlgdata, 'output');
                dlgdata = TLManageScalingCtrls( ...
                    'update', pageNum, dlgdata, 'output', []);
                % set controls
                TLManageVariableCtrls('setCtrls', pageNum, dlgfig, dlgdata);
                TLManageScalingCtrls('setCtrls', pageNum, dlgfig, dlgdata);

            case 'Threshold'
                dlgdata = FcnUpdateThresholdControls(dlgdata, dlgfig, pageNum);

            case 'Logging & Autoscaling'
                dlgdata =  TLManageLoggingCtrls('update',pageNum,dlgdata,'logdata','output');
                TLManageLoggingCtrls('setCtrls',pageNum, dlgfig, dlgdata);

            case 'Documentation'
                TLManageDocCtrls('update', pageNum, dlgfig, dlgdata);
        end
        varargout{1} = dlgdata;

    case 'Apply'
        dlgdata  = varargin{2};
        bClose   = varargin{3};
        [dlgdata, bClose] = tl_apply_blockdata(dlgdata, bClose);
        varargout{1} = dlgdata;
        varargout{2} = bClose;

    otherwise
        error(['Invalid action: "' action '"']);
end


%-----------------------------------------------------------------------------------------
% FcnManageSystemIcons manage actions of toolbar icons
%
% INPUT ARGUMENTS
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%   action      action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%   bModified   flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata, bModified] = FcnManageSystemIcons(dlgfig, dlgdata, action) %#ok

bModified = false;
switch action
    case 'EditCode'
        % look for associated production code file
        [codefile,blockstr] = get_codefile(dlgdata.model);
        if ~isempty(codefile)
            codeedit(codefile,blockstr);
        end

    case 'ViewCode'
        tl_create_links_to_model('ShowComputation',dlgdata.model, 'output');

    otherwise
        error('Invalid callback');

end % switch(action)


%-----------------------------------------------------------------------------------------
% FcnCalcThresholdControlPositions calculates positions for all controls on threshold page
%
% INPUT ARGUMENTS
%   commonGeom      common geometry information
%
% OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% HINT:
% Positions are calculated from the bottom left of the figure to avoid
% dependencies on figure size
%-----------------------------------------------------------------------------------------
function [sheetDims, ctrlPos] = FcnCalcThresholdControlPositions(commonGeom)

excludedCtrls = {'calcranges', 'inheritproperties', 'saturate' };
[sheetDims, ctrlPos, col, colDelta, rowDelta] = ...
    tl_calc_ctrls_positions(commonGeom, excludedCtrls);

strings_and_styles = {...
    'Threshold:',                'text';
    'Scalar',                    'checkbox';
    'Criteria for first input:', 'text';
    'u2 >= Threshold',           'popupmenu'; ...
    'Use value of Data Dictionary variable',    'checkbox'};

[thresholdLabelSize, ...
    scalarCheckboxSize, ...
    criteriaLabelSize, ...
    criteriaPopupSize, ...
    useDDValueCheckboxSize] = ...
    ds_get_uicontrol_sizes(commonGeom, strings_and_styles);

% define new height for addoptGroupbox
ctrlPos.addoptGroupbox(4) = commonGeom.editHeight ...
    + commonGeom.checkboxHeight ...
    + rowDelta ...
    + commonGeom.frameBottomEdgeBuffer ...
    + commonGeom.frameTopEdgeBuffer;

%---------------------------------------
% 2nd row
cy = commonGeom.frameBottomEdgeBuffer;

% ddv checkbox
cx = col(2);
ctrlPos.useDDValueCheckbox = [cx, cy, useDDValueCheckboxSize];

%------------
% 1st row
cy = cy + commonGeom.checkboxHeight + rowDelta;

% threshold label
cx = col(1);
ctrlPos.thresholdLabel = [cx cy thresholdLabelSize];

% threshold edit field
cx = col(2);
ctrlPos.thresholdEdit = [cx cy ctrlPos.varEdit(3:4)];

% scalar checkbox
cx = col(4);
ctrlPos.scalarCheckbox = [cx cy scalarCheckboxSize];

% criteria label
cx = col(6);
ctrlPos.criteriaLabel = [cx cy criteriaLabelSize];

% criteria popup
cx = col(6) + criteriaLabelSize(1) + fix(colDelta/2);
ctrlPos.criteriaPopup = [cx cy criteriaPopupSize];

% increase sheet dimensions
sheetDims(2) = sheetDims(2) ...
    + ctrlPos.addoptGroupbox(4) ...
    + commonGeom.textHeight;


%-----------------------------------------------------------------------------------------
% FcnCreateThresholdControls creates the figure window
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%-----------------------------------------------------------------------------------------
function dlgdata = FcnCreateThresholdControls(pageNum,dlgfig,dlgdata)

% invoke standard function to create variable controls
dlgdata = TLManageVariableCtrls('create', pageNum, dlgfig, dlgdata);
dlgdata = TLManageScalingCtrls('create', pageNum, dlgfig, dlgdata);

Children = dlgdata.pageData{pageNum}.children;
ctrlPos  = dlgdata.pageData{pageNum}.ctrlPos;
pageName = dlgdata.pageNames{pageNum};

% create the addoptGroupbox which will contain threshold value
Children.addoptGroupbox = uipanel( ...
    'Parent',            dlgfig, ...
    'Title',            {'Threshold'}, ...
    'Units',            'pixels', ...
    'Position',          ctrlPos.addoptGroupbox);

Children.thresholdLabel = uicontrol( ...
    'Parent',             Children.addoptGroupbox, ...
    'Style',              'text', ...
    'String',             'Threshold:', ...
    'Position',           ctrlPos.thresholdLabel);

Children.thresholdEdit = uicontrol( ...
    'Parent',             Children.addoptGroupbox, ...
    'BackgroundColor',    'w', ...
    'Style',              'edit', ...
    'Position',           ctrlPos.thresholdEdit, ...
    'Callback',           sprintf('tldlg(''%s'',gcbf,''thresholdEdit'')',pageName));

Children.scalarCheckbox = uicontrol( ...
    'Parent',             Children.addoptGroupbox, ...
    'Style',              'checkbox', ...
    'String',             'Scalar', ...
    'Position',           ctrlPos.scalarCheckbox, ...
    'Callback',           sprintf('tldlg(''%s'',gcbf,''scalarCheckbox'')',pageName));

Children.criteriaLabel = uicontrol( ...
    'Parent',             Children.addoptGroupbox, ...
    'Style',              'Text', ...
    'String',             'Criteria for first input:', ...
    'Position',           ctrlPos.criteriaLabel);

Children.criteriaPopup = uicontrol( ...
    'Parent',             Children.addoptGroupbox, ...
    'Style',              'popupmenu', ...
    'String',             {'u2 >= Threshold'}, ...
    'Value',              1, ...
    'Backgroundcolor',    [1 1 1], ...
    'Position',           ctrlPos.criteriaPopup, ...
    'Callback',           sprintf('tldlg(''%s'',gcbf,''criteriaPopup'')',pageName));

Children.useDDValueCheckbox = uicontrol( ...
    'Parent',             Children.addoptGroupbox, ...
    'Style',              'checkbox', ...
    'String',             'Use value of Data Dictionary variable', ...
    'Position',           ctrlPos.useDDValueCheckbox, ...
    'Callback',           sprintf('tldlg(''%s'',gcbf,''useDDValueCheckbox'')',pageName));

dlgdata.pageData{pageNum}.children = Children;


%-----------------------------------------------------------------------------------------
% FcnManageThresholdControls manage control actions of threshold page
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%   pageAction  action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%   bModified   flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata, bModified] = FcnManageThresholdControls(pageNum, dlgfig, dlgdata, pageAction)

Children = dlgdata.pageData{pageNum}.children;
switch pageAction
    case 'thresholdEdit'
        thresholdStr = deblank(get(Children.thresholdEdit, 'String'));
        if ~isempty(thresholdStr)
            if strcmp(thresholdStr, 'ddv')
                if isempty(dlgdata.data.threshold.variable)
                    beep;
                else
                    dlgdata.data.threshold.value = ['ddv(''', dlgdata.data.threshold.variable, ''')'];
                    dlgdata.data.threshold.useddvariablevalue = true;
                end
            else
                dlgdata.data.threshold.value = thresholdStr;
            end
        else
            beep;
        end
        bModified = true;

    case 'scalarCheckbox'
        dlgdata.data.threshold.scalar = get(Children.scalarCheckbox, 'Value');
        bModified = true;

    case 'criteriaPopup'
        dlgdata.data.threshold.criteria = get(Children.criteriaPopup, 'Value');
        bModified = true;

    case 'useDDValueCheckbox'
        if get(Children.useDDValueCheckbox, 'Value')
            dlgdata.data.threshold.value = ['ddv(''', dlgdata.data.threshold.variable, ''')'];
            dlgdata.data.threshold.useddvariablevalue = true;
        else
            dlgdata.data.threshold.useddvariablevalue = false;
        end
        bModified = true;

    otherwise
        [dlgdata, bModified1] = ...
            TLManageVariableCtrls('manage',pageNum,dlgfig,dlgdata,pageAction,'threshold');
        [dlgdata, bModified2] = ...
            TLManageScalingCtrls('manage',pageNum,dlgfig,dlgdata,pageAction,'threshold');
        bModified = bModified1 || bModified2;
end


%--------------------------------------------------------------------------
% FcnUpdateThresholdControls updates the controls of the threshold tab
%
% INPUT ARGUMENTS
%   dlgdata     dialog data
%   dlgfig      figure handle of dialog
%   pageNum     page Number
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%--------------------------------------------------------------------------
function dlgdata = FcnUpdateThresholdControls(dlgdata,dlgfig,pageNum)

enumStruct = tl_manage_blockset('GetEnums');
idx = strcmp({enumStruct.name},'ThresholdCriteriaEnum');
thresholdCriteriaEnums = enumStruct(idx).strings; 

% default control status for controls of threshold value groupbox
thresholdEditCtrlStatus = struct('enable', 'on', 'string', '');
criteriaPopupCtrlStatus = struct('enable', 'on' , ...
    'string', {thresholdCriteriaEnums}, ...
    'value', 1);

scalarCheckboxCtrlStatus = struct('enable', 'on', 'value', 0);
if strcmp(tl_deblank(dlgdata.data.threshold.class, 2), 'default')
    dlgdata.data.threshold.scalar = 0;
    scalarCheckboxCtrlStatus.enable = 'off';
elseif dlgdata.data.output.inheritscaling == 1 
    % this reflects the static synchronization rule (see tl_switch_api.m) 
    % that property inheritance implies scalar limits 
    scalarCheckboxCtrlStatus.enable = 'inactive'; 
end 

if dlgdata.data.threshold.scalar
    dlgdata.extdata.threshold.scalarExp = 1;
end

% update variable controls (set UserData property). Here, extdata is set
% and contains information about scalar exansion flag which is needed for
% the determination of the contraints
dlgdata = TLManageVariableCtrls('update', pageNum, dlgdata, 'threshold');

[thresholdValue, constraint, bCompliant] = tl_get_param_constraints(dlgdata.data.threshold, dlgdata.extdata.threshold);

% update scaling controls
dlgdata = TLManageScalingCtrls('update', pageNum, dlgdata, ...
    'threshold', constraint);

% adjust some data
thresholdEditCtrlStatus.string = dlgdata.data.threshold.value;
if dlgdata.data.threshold.useddvariablevalue
    thresholdEditCtrlStatus.enable = 'inactive';
end
thresholdEditCtrlStatus.iserror = ~bCompliant;

% with criteria 3 (u1 ~= 0) the threshold is given implicitly
bImplicit = dlgdata.data.threshold.criteria == 3;
if bImplicit
    thresholdEditCtrlStatus.string = '0';
    thresholdEditCtrlStatus.enable = 'inactive';
    thresholdEditCtrlStatus.iserror = false;
else
    thresholdEditCtrlStatus.string = dlgdata.data.threshold.value;
    if dlgdata.data.threshold.useddvariablevalue
        thresholdEditCtrlStatus.enable = 'inactive';
    end
    thresholdEditCtrlStatus.iserror = ~bCompliant;
end

% status of ddv checkbox
useDDValueCheckboxCtrlStatus.enable = flag2str(~isempty(dlgdata.data.threshold.variable));
useDDValueCheckboxCtrlStatus.value = dlgdata.data.threshold.useddvariablevalue;

scalarCheckboxCtrlStatus.value = dlgdata.data.threshold.scalar;
criteriaPopupCtrlStatus.value = dlgdata.data.threshold.criteria;

% for parameters there are no simulated ranges. Instead, write min/max of
% threshold value
Children = dlgdata.pageData{pageNum}.children;
simulatedLabelCtrlStatus = tl_get_control_userdata(Children.simulatedLabel);
upperSimulatedValueCtrlStatus = tl_get_control_userdata(Children.upperSimulatedValue);
lowerSimulatedValueCtrlStatus = tl_get_control_userdata(Children.lowerSimulatedValue);
if dlgdata.extdata.threshold.scalarExp
    simulatedLabelCtrlStatus.string = 'of vector';
else
    simulatedLabelCtrlStatus.string = 'of element';
end

widthEditCtrlStatus = tl_get_control_userdata(Children.widthEdit);
widthUpButtonCtrlStatus = tl_get_control_userdata(Children.widthUpButton);
widthDownButtonCtrlStatus = tl_get_control_userdata(Children.widthDownButton);
elementEditCtrlStatus = tl_get_control_userdata(Children.elementEdit);
elementUpButtonCtrlStatus = tl_get_control_userdata(Children.elementUpButton);
elementDownButtonCtrlStatus = tl_get_control_userdata(Children.elementDownButton);
if dlgdata.data.output.inheritscaling
    widthEditCtrlStatus.enable = 'off';
    widthEditCtrlStatus.string = '';
    widthUpButtonCtrlStatus.enable = 'off';
    widthDownButtonCtrlStatus.enable = 'off';
    elementEditCtrlStatus.enable = 'off';
    elementEditCtrlStatus.string = '';
    elementUpButtonCtrlStatus.enable = 'off';
    elementDownButtonCtrlStatus.enable = 'off';
end

% if "scalar" is selected, scalar expansion flag is disabled
scalarExpCheckboxCtrlStatus = tl_get_control_userdata(Children.scalarExpCheckbox);
if dlgdata.data.threshold.scalar
    scalarExpCheckboxCtrlStatus.enable = 'off';
end

% check and set status of threshold value groupbox controls and the min/max of the threshold value
if ~strcmp(tl_deblank(dlgdata.data.threshold.class, 2), 'default')
    % fill row with simulated values
    if ~isempty(constraint)
        lowerSimulatedValueCtrlStatus.string = sprintf('%0.6g', min(constraint));
        upperSimulatedValueCtrlStatus.string = sprintf('%0.6g', max(constraint));
    else
        lowerSimulatedValueCtrlStatus.string = 'n.a.';
        upperSimulatedValueCtrlStatus.string = 'n.a.';
    end
    % else
    %    scalarCheckboxCtrlStatus.enable = 'off';
end

% disable all controls (except threshold and criteria) if criterion 'u2 ~= 0' is selected
if bImplicit
    thresChildren = rmfield(Children, 'criteriaPopup');
    thresChildren = rmfield(thresChildren, 'thresholdEdit');
    ctrlsList = fieldnames(thresChildren);
    ctrlsList = ctrlsList(cellfun('isempty', regexp(ctrlsList, 'Groupbox', 'match')));
    ctrlsList = ctrlsList(cellfun('isempty', regexp(ctrlsList, 'Label', 'match')));
    ctrlsList = [ctrlsList; {'implementedLabel'; 'simulatedLabel'}];
    for ctrlsNo=1:length(ctrlsList)
        ctrl = Children.(ctrlsList{ctrlsNo});
        UserData = get(ctrl, 'UserData');
        UserData.ctrlStatus.enable = 'off';
        set(ctrl, 'UserData', UserData);
    end
    TLSetCtrlsStatus(dlgfig, Children, ctrlsList);
    % special handling of threshold and criteria
    extraCtrlsList = {
        'thresholdEdit', ...
        'criteriaPopup'};
    for ctrlsNo=1:length(extraCtrlsList)
        ctrl = Children.(extraCtrlsList{ctrlsNo});
        UserData = get(ctrl, 'UserData');
        UserData.ctrlStatus = eval([extraCtrlsList{ctrlsNo} 'CtrlStatus']);
        set(ctrl, 'UserData', UserData);
    end
    TLSetCtrlsStatus(dlgfig, Children, extraCtrlsList);
else
    % write back control status to the controls' UserData property
    extraCtrlsList = {
        'thresholdEdit', ...
        'scalarCheckbox', ...
        'criteriaPopup', ...
        'useDDValueCheckbox'};
    standardCtrlsList = {
        'simulatedLabel', ...
        'upperSimulatedValue', ...
        'lowerSimulatedValue', ...
        'scalarExpCheckbox', ...
        'widthEdit', ...
        'widthDownButton', ...
        'widthUpButton', ...
        'elementEdit', ...
        'elementDownButton', ...
        'elementUpButton'
        };
    ctrlsList = [extraCtrlsList standardCtrlsList];
    for ctrlsNo=1:length(ctrlsList)
        ctrl = Children.(ctrlsList{ctrlsNo});
        UserData = get(ctrl, 'UserData');
        UserData.ctrlStatus = eval([ctrlsList{ctrlsNo} 'CtrlStatus']);
        set(ctrl, 'UserData', UserData);
    end
    % set control status of variable, scaling and gain parameter groupbox controls
    TLManageVariableCtrls('setCtrls', pageNum, dlgfig, dlgdata);
    TLManageScalingCtrls('setCtrls', pageNum, dlgfig, dlgdata);
    TLSetCtrlsStatus(dlgfig,Children, extraCtrlsList);
end
